{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Sorting" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This week's lab explores different algorithms for sorting.\n", "\n", "* You can sort any type of data, as long as they are **comparable**\n", "* These sorting algorithms are \"in place\": they don't use additional memory" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.1 Double-Trouble Sort" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compare each item with all others, and swap them if they are in the wrong order." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.2 Bubble Sort" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compare each item with the next, and swap them if you must. Repeat until no swaps are made." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.3 Quicksort" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Divide the list in half and quicksort the halves. Combine the halves." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 2. Files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.1 Writing Text Files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Writing to a file can cause an error (and Exception) and you must \"catch it\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, we import some IO functions:\n", "\n", "* FileWriter - does the low-level writing\n", "* BufferedWriter - wraps around FileWriter\n", "* FileReader - does the low-level reading\n", "* BufferedReader - wraps around the FileReader\n", "\n", "Reading and writing is slow, if you do it character-by-character. If you can handle a bunch at once, that is more efficient. The \"buffer\" adds a place to store a bunch of characters coming in or out." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "import java.io.*;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A writer provides a method `write(String)`. Note that it doesn't add a newline on the end. You have to do that manually.\n", "\n", "Most importantly, Java requires that you \"catch\" any errors (eg, Exceptions) that can happen. If you don't catch them, you can't compile the code. (Java9 interpreter actually allows you to skip this part.)\n", "\n", "When you catch it, you can do \"handle the problem.\" \n", "\n", "The lower level code must be \"throwing\" these Exceptions." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added variable fileName of type String with initial value \"temp.txt\"\n", "\n", "\n", "\n" ] } ], "source": [ "String fileName = \"temp.txt\";\n", "\n", "try {\n", " FileWriter fileWriter = new FileWriter(fileName);\n", "\n", " BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);\n", "\n", " bufferedWriter.write(\"Hello there,\");\n", " bufferedWriter.write(\" here is some text.\");\n", " bufferedWriter.newLine();\n", " bufferedWriter.write(\"We are writing\");\n", " bufferedWriter.write(\" the text to the file.\");\n", "\n", " bufferedWriter.close();\n", "} catch(IOException ex) {\n", " System.out.println(\"Error writing to file '\" + fileName + \"'\");\n", "}\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.2 Reading Text Files\n", "\n", "Reading works " ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Modified variable line of type String with initial value null\n", "\n", "\n", " \n", " } catch(IOException ex) {\n", " \n", " }\n", "Hello there, here is some text.\n", "We are writing the text to the file.\n", "\n" ] } ], "source": [ "String line = null;\n", "\n", "try {\n", " FileReader fileReader = new FileReader(fileName);\n", " BufferedReader bufferedReader = new BufferedReader(fileReader);\n", "\n", " while((line = bufferedReader.readLine()) != null) {\n", " System.out.println(line);\n", " } \n", "\n", " bufferedReader.close(); \n", "} catch(FileNotFoundException ex) {\n", " System.out.println(\"Unable to open file '\" + fileName + \"'\"); \n", "} catch(IOException ex) {\n", " System.out.println(\"Error reading file '\" + fileName + \"'\"); \n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.3 Compiling Java Programs" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [], "source": [ "! javac *.java" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "! java Game Game.game" ] } ], "metadata": { "kernelspec": { "display_name": "Java 9", "language": "java", "name": "java9" }, "language_info": { "file_extension": ".class", "mimetype": "application/java-vm", "name": "java" } }, "nbformat": 4, "nbformat_minor": 0 }